<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Offline extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'));
        $this->data['controller'] = 'offline';
        $this->load->model('Moffline');
    }
    
    public function index()
    {
        $this->data['cat'] = $cat = $this->getParamInt('cat');
        $this->load->library('pagination');
        $config['per_page'] = 30;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(3,3);
        $offset = (($this->getParamUri(3,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(3,3) - 1) * $config['per_page']) : 0;
        $data = $this->Moffline->getIndex($config['per_page'],$offset,$cat);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,3);
        $config['suffix'] = '.html';
        $config['use_page_numbers'] = TRUE;
        
        if(!in_array($cat, array(null, '', '0'))){
            $config['paramhack'] = '?cat='.$cat;
        }
        
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        $this->data['count'] = $data['count'];
        
        $this->data['action'] = 'index';
        $this->load->view('template/layout', $this->data);
    }
    
    public function addQuestion()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('question', 'Nội dung câu hỏi', 'required');
        $this->form_validation->set_rules('choice-1', 'Đáp án 1', 'required');
        $this->form_validation->set_rules('choice-2', 'Đáp án 2', 'required');
        $this->form_validation->set_rules('choice-3', 'Đáp án 3', 'required');
        $this->form_validation->set_rules('choice-4', 'Đáp án 4', 'required');
        $this->form_validation->set_rules('answer', 'Đáp án đúng', 'required');
        
        $this->data['data'] = '';
        $this->data['quest'] = $this->Moffline->getQuestionCate();
		$this->data['list_question'] = $this->Moffline->list_question();
        if($this->form_validation->run() === TRUE)
        {
            $cat_quest = $this->getParamString('cat_q');
            $question = $this->getParamString('question');
            $choice1 = $this->getParamString('choice-1');
            $choice2 = $this->getParamString('choice-2');
            $choice3 = $this->getParamString('choice-3');
            $choice4 = $this->getParamString('choice-4');
            $answer = $this->getParamString('answer');
            
            $this->data['data'] = $this->Moffline->addThisQuestion($cat_quest,$question,$choice1,$choice2,$choice3,$choice4,$answer);
            if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm câu hỏi thành công!");window.location.href="'.$this->data['baseurl'].'manage/offline-question/add-question/";</script>');
            }
        }
        
        $this->data['action'] = 'addquestion';
        $this->load->view('template/layout',$this->data);
    }
    
    public function editQuestion()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('question', 'Nội dung câu hỏi', 'required');
        $this->form_validation->set_rules('choice-1', 'Đáp án 1', 'required');
        $this->form_validation->set_rules('choice-2', 'Đáp án 2', 'required');
        $this->form_validation->set_rules('choice-3', 'Đáp án 3', 'required');
        $this->form_validation->set_rules('choice-4', 'Đáp án 4', 'required');
        $this->form_validation->set_rules('answer', 'Đáp án đúng', 'required');
        
        $id = $this->getParamUri(4,0);
        $this->data['data'] = $this->Moffline->getThisQuestion($id);
        $this->data['quest'] = $this->Moffline->getQuestionCate();
        if($this->form_validation->run() === TRUE)
        {
            $this->data['id_cat'] = $cat_quest = $this->getParamString('cat_q');
            $question = $this->getParamString('question');
            $choice1 = $this->getParamString('choice-1');
            $choice2 = $this->getParamString('choice-2');
            $choice3 = $this->getParamString('choice-3');
            $choice4 = $this->getParamString('choice-4');
            $answer = $this->getParamString('answer');
            
            $this->data['data'] = $this->Moffline->editThisQuestion($cat_quest,$id,$question,$choice1,$choice2,$choice3,$choice4,$answer);
            if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa câu hỏi thành công!");window.location.href="'.$this->data['baseurl'].'manage/offline-question/edit/'.$id.'";</script>');
            }
        }
        
        $this->data['action'] = 'editquestion';
        $this->load->view('template/layout',$this->data);
    }
    
    public function deleteQuestion()
    {
        $id = $this->getParamInt('id');
        $this->data['delete'] = $this->Moffline->deleteThisQuestion($id);
    }
	
	public function check_content_question(){
		$question = $this->getParamString('question');
        $this->data['check_content_question'] = $this->Moffline->check_content_question($question);
		$this->load->view('template/offline/check_content_question',$this->data);
	}
	
}
